﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastReport;
using MINERP.BLL.DisjoinProForOutShop;
using MINERP.MODEL.FoundationModel;
using System.IO;

namespace MINERP.DisjoinProForOutShop.Forms
{
    public partial class FrmShipping : Form
    {
        public FrmShipping()
        {
            InitializeComponent();
        }
        public FrmShipping(int inOrderBagID)
        {
            InitializeComponent();
            OrderBageid = inOrderBagID;
            //AutoPreView = true;
        }
        ShippingBLL _bll = new ShippingBLL();
        private int OrderBageid = 0;

        private void FrmShipping_Load(object sender, EventArgs e)
        {
            textEditBagId.Text = OrderBageid.ToString();
            _bll.AskDel += new ShippingBLL.ASK_DEL(_bll_AskDel);
        }
        ShippingView CurrShippingView = new ShippingView();

        void PrintShipping(ShippingView inShippingView)
        {
            Report report = new Report();

            //report.Load(shippingView.ShippingCompany.ShippingFormatPath);
            report.Load(Application.StartupPath + inShippingView.ShippingCompany.ShippingFormatPath);

            report.Preview = previewControl1;
            report.RegisterData(newList, "shippingView");
            if (inShippingView.ShippingCompany.Alias.ToUpper().Equals("MAIL") && inShippingView.CustomAddress.TotalPrice != -1)
            {
                report.SetParameterValue("IsPrintPrice", true);
            }
            else
            {
                report.SetParameterValue("IsPrintPrice", false);
            }

            //report.Design();
            //report.Dispose();
            previewControl1.Refresh();
            previewControl1.Show();
            report.Show();

          
        }

        void PrintBill(ShippingView inShippingView)
        {
            if (CurrShippingView.CustomAddress.DeliveryReportStream == null)
            {
                MessageBox.Show("账单未找到", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                File.WriteAllBytes(Application.StartupPath + " \\Report\\c.fpx", CurrShippingView.CustomAddress.DeliveryReportStream);
                Report report = new Report();
                report.LoadPrepared(Application.StartupPath + " \\Report\\c.fpx");
                report.ShowPrepared();
            }
        }
        List<ShippingView> newList = new List<ShippingView>();
        private void simpleButtonPreview_Click(object sender, EventArgs e)
        {
            //CurrShippingView = null;
            //newList = new List<ShippingView>();
            //ShippingView shippingView = new ShippingView();
            //if ((!string.IsNullOrEmpty(textEditBagId.Text) && Convert.ToInt32(textEditBagId.Text.Trim()) != 0) && (string.IsNullOrEmpty(textEditOrderproductid.Text)||Convert.ToInt32(textEditOrderproductid.Text.Trim()) == 0))
            //{
            //    shippingView = _bll.GetShippingInfo(Convert.ToInt32(textEditBagId.Text));

            //    if (shippingView != null)
            //    {
            //        CurrShippingView = shippingView;
            //        newList.Add(shippingView);
            //        PrintShipping(shippingView);
            //        if (checkBoxAutoPrint.Checked)
            //        {
            //            PrintBill(shippingView);
            //        }
            //    }
            //    else
            //        MessageBox.Show("未找到该订单，请确认", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);



            //}
            //else if ((string.IsNullOrEmpty(textEditBagId.Text)||Convert.ToInt32(textEditBagId.Text.Trim()) == 0) &&(!string.IsNullOrEmpty(textEditOrderproductid.Text)&& Convert.ToInt32(textEditOrderproductid.Text.Trim())!= 0))
            //{
            //    shippingView = _bll.GetShippingInfoByProductid(Convert.ToInt32(textEditOrderproductid.Text));
            //    if (shippingView != null)
            //    {
            //        CurrShippingView = shippingView;
            //        newList.Add(shippingView);
            //        PrintShipping(shippingView);
            //        if (checkBoxAutoPrint.Checked)
            //        {
            //            PrintBill(shippingView);
            //        }


                   
            //    }
            //    else
            //        MessageBox.Show("未找到该订单，请确认", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
            //else if ((!string.IsNullOrEmpty(textEditBagId.Text)&&Convert.ToInt32(textEditBagId.Text.Trim()) != 0) && Convert.ToInt32(textEditOrderproductid.Text.Trim()) != 0)
            //{

            //    shippingView = _bll.GetShippingInfo(Convert.ToInt32(textEditBagId.Text));
            //    if (shippingView != null)
            //    {
            //        CurrShippingView = shippingView;
            //        newList.Add(shippingView);
            //        PrintShipping(shippingView);
            //        if (checkBoxAutoPrint.Checked)
            //        {
            //            PrintBill(shippingView);
            //        }

            //    }
            //    else
            //        MessageBox.Show("未找到该订单，请确认", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
            //else
            //{
            //    //MessageBox.Show("请先预览该快递单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
            //if (CurrShippingView != nullCurrShippingView.CustomAddress != null)
            //{
            //    //_bll.AskDel += new ShippingBLL.ASK_DEL(_bll_AskDel);
            //    if (!string.IsNullOrEmpty(textEditKuaiDi.Text.Trim()))
            //    {
            //        _bll.ReflectToIPG(CurrShippingView, textEditKuaiDi.Text.Trim());
            //    }
            //    else
            //    {
            //        MessageBox.Show("请先输入快递单号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    }
            //}
            //else
            //{

            //}
            CurrShippingView = _bll.GetShippingInfo(Convert.ToInt32(textEditBagId.Text));
            if (CurrShippingView != null&&CurrShippingView.CustomAddress != null)
            {

               // newList.Add(CurrShippingView);
                PrintShipping(CurrShippingView);
                if (checkBoxAutoPrint.Checked)
                {
                    PrintBill(CurrShippingView);
                }
                if (!string.IsNullOrEmpty(textEditKuaiDi.Text.Trim()))
                {
                    _bll.ReflectToIPG(CurrShippingView, textEditKuaiDi.Text.Trim());
                }
                else
                {
                    MessageBox.Show("请先输入快递单号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
        }

        private void simpleButtonOk_Click(object sender, EventArgs e)
        {
            if (CurrShippingView != null && CurrShippingView.CustomAddress != null)
            {
              
                if (!string.IsNullOrEmpty(textEditKuaiDi.Text.Trim()))
                {
                    _bll.ReflectToIPG(CurrShippingView, textEditKuaiDi.Text.Trim());
                }
                else
                {
                    MessageBox.Show("请先输入快递单号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
              
            }
        }

        bool _bll_AskDel()
        {
            if (MessageBox.Show("该客人的反馈文件已存在是否替换？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) ==
                DialogResult.OK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (CurrShippingView == null)
            {
                CurrShippingView = _bll.GetShippingInfo(Convert.ToInt32(textEditBagId.Text));
            }
            if (CurrShippingView != null)
            {
                PrintBill(CurrShippingView);
            }
            else
            {
                MessageBox.Show("未找到该订单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void textEditKuaiDi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                simpleButtonPreview_Click(null,null);
            }
        }

        private void simpleButtonSelect_Click(object sender, EventArgs e)
        {
            CurrShippingView = null;
            newList = new List<ShippingView>();
            ShippingView shippingView = new ShippingView();
            if ((!string.IsNullOrEmpty(textEditBagId.Text) && Convert.ToInt32(textEditBagId.Text.Trim()) != 0) && (string.IsNullOrEmpty(textEditOrderproductid.Text) || Convert.ToInt32(textEditOrderproductid.Text.Trim()) == 0))
            {
                shippingView = _bll.GetShippingInfo(Convert.ToInt32(textEditBagId.Text));

                if (shippingView != null || string.IsNullOrEmpty(shippingView.ShippingCompany.ShippingCompanyName))
                {
                    CurrShippingView = shippingView;
                    newList.Add(shippingView);
                    PrintShipping(shippingView);
                    if (checkBoxAutoPrint.Checked)
                    {
                        PrintBill(shippingView);
                    }
                    this.textEditShippingCompany.Text = shippingView.ShippingCompany.ShippingCompanyName;
                }
                else
                    MessageBox.Show("未找到该订单，请确认", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);



            }
            else if ((string.IsNullOrEmpty(textEditBagId.Text) || Convert.ToInt32(textEditBagId.Text.Trim()) == 0) && (!string.IsNullOrEmpty(textEditOrderproductid.Text) && Convert.ToInt32(textEditOrderproductid.Text.Trim()) != 0))
            {
                shippingView = _bll.GetShippingInfoByProductid(Convert.ToInt32(textEditOrderproductid.Text));
                if (shippingView != null && !string.IsNullOrEmpty(shippingView.ShippingCompany.ShippingCompanyName))
                {
                    CurrShippingView = shippingView;
                    newList.Add(shippingView);
                    PrintShipping(shippingView);
                    if (checkBoxAutoPrint.Checked)
                    {
                        PrintBill(shippingView);
                    }
                    this.textEditShippingCompany.Text = shippingView.ShippingCompany.ShippingCompanyName;



                }
                else
                    MessageBox.Show("未找到该订单的发货信息 ，请确认", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else if ((!string.IsNullOrEmpty(textEditBagId.Text) && Convert.ToInt32(textEditBagId.Text.Trim()) != 0) && Convert.ToInt32(textEditOrderproductid.Text.Trim()) != 0)
            {

                shippingView = _bll.GetShippingInfo(Convert.ToInt32(textEditBagId.Text));
                if (shippingView != null || string.IsNullOrEmpty(shippingView.ShippingCompany.ShippingCompanyName))
                {
                    CurrShippingView = shippingView;
                    newList.Add(shippingView);
                    PrintShipping(shippingView);
                    if (checkBoxAutoPrint.Checked)
                    {
                        PrintBill(shippingView);
                    }
                    this.textEditShippingCompany.Text = shippingView.ShippingCompany.ShippingCompanyName;

                }
                else
                    MessageBox.Show("未找到该订单，请确认", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                //MessageBox.Show("请先预览该快递单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            //if (CurrShippingView != null && CurrShippingView.CustomAddress != null)
            //{
            //    //_bll.AskDel += new ShippingBLL.ASK_DEL(_bll_AskDel);
            //    if (!string.IsNullOrEmpty(textEditKuaiDi.Text.Trim()))
            //    {
            //        _bll.ReflectToIPG(CurrShippingView, textEditKuaiDi.Text.Trim());
            //    }
            //    else
            //    {
            //        MessageBox.Show("请先输入快递单号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    }
            //}
            //else
            //{

            //}
        }

        private void textEditOrderproductid_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue==(int)Keys.Enter)
            {
                simpleButtonSelect_Click(null,null);
            }
        }

        
    }
}
